Option Explicit

Const vbext_ct_ClassModule = 2
Const vbext_ct_Document = 100
Const vbext_ct_MSForm = 3
Const vbext_ct_StdModule = 1
Const fsoForReading = 1
Const xlExcel8 = 56

Main()

Sub Main()
    Dim xlapp, fso, ext, folder, files, file, argumentfolder, wb, ws, xlFileName, fileStream, VBComp, compName, code
    
    If wScript.Arguments.Count <> 1 Then
        MsgBox "As the only argument, give the folder to build the Excel file."
    Else
        
      argumentfolder = Trim(wScript.Arguments(0))

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set folder = fso.GetFolder(argumentfolder)
      
      'Create Excel file
      Set xlapp = CreateObject("Excel.Application")
      xlapp.Visible = True
      Set wb = xlapp.Workbooks.Add()

      'Import code files
      Set files = folder.files
      For Each file In files
          ext = fso.GetExtensionName(file.Path)
          Select Case ext
            Case "wbc", "shc"
                compName = fso.GetBaseName(file)
                On Error Resume Next
                Err.Clear
                Set VBComp = wb.VBProject.VBComponents(compName)
                If Err.Number <> 0 Then
                    On Error GoTo 0
                    Set ws = wb.Worksheets.Add()
                    WScript.Sleep 100
                    'ws.Name = compName
                    wb.VBProject.VBComponents(ws.CodeName).Name = compName
                End If
                On Error GoTo 0
                Set fileStream = fso.OpenTextFile(file.Path, fsoForReading)
				if Not fileStream.AtEndOfStream Then code = fileStream.ReadAll
				fileStream.Close
                If code<> "" Then wb.VBProject.VBComponents(compName).CodeModule.InsertLines 1, code
            Case "cls", "frm", "bas"
				On Error Resume Next
                wb.VBProject.VBComponents.Import file.Path
				If Err.Number <> 0 Then MsgBox "Could not import file " & file.Name & vbLf & Err.Description
				On Error GoTo 0
          End Select
      Next
      
      'Save excel file
      xlFileName = argumentfolder & ".xls"
      If fso.FileExists(xlFileName) Then fso.DeleteFile xlFileName
      xlapp.ActiveWorkbook.SaveAs xlFileName, xlExcel8

      xlapp.Quit
	  
	End If
	
End Sub